package ytdp.careagle.com.ytdp.activity;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umeng.analytics.dplus.UMADplus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import ytdp.careagle.com.ytdp.R;
import ytdp.careagle.com.ytdp.adapter.base.CommonAdapter;
import ytdp.careagle.com.ytdp.adapter.base.MultiItemTypeAdapter;
import ytdp.careagle.com.ytdp.adapter.base.ViewHolder;
import ytdp.careagle.com.ytdp.app.APP;
import ytdp.careagle.com.ytdp.app.Operation;
import ytdp.careagle.com.ytdp.app.manager.ImageLoadManager;
import ytdp.careagle.com.ytdp.entity.PushCarEntity;
import ytdp.careagle.com.ytdp.entity.QueryAllCarType;
import ytdp.careagle.com.ytdp.entity.SellCarProgramEntity;
import ytdp.careagle.com.ytdp.entity.ValuationQuery;
import ytdp.careagle.com.ytdp.mvp.presenter.EvaluateResultPresenter;
import ytdp.careagle.com.ytdp.mvp.view.IEvaluateResultView;
import ytdp.careagle.com.ytdp.myview.MyChartLine;
import ytdp.careagle.com.ytdp.myview.MySectorSelectView;
import ytdp.careagle.com.ytdp.myview.NumAnim;
import ytdp.careagle.com.ytdp.utils.Utils;

public class EvaluateResultActivity extends BaseActivity implements IEvaluateResultView, MySectorSelectView.StateChangeListener, MultiItemTypeAdapter.OnItemClickListener {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_subtitle)
    TextView tvSubtitle;
    @Bind(R.id.tv_car_type)
    TextView tvCarType;
    @Bind(R.id.tv_car_info)
    TextView tvCarInfo;
    @Bind(R.id.tv_new_car_price)
    TextView tvNewCarPrice;
    @Bind(R.id.myselectview)
    MySectorSelectView myselectview;
    @Bind(R.id.myCharLine)
    MyChartLine myCharLine;
    @Bind(R.id.tv_sell_car)
    TextView tvSellCar;
    @Bind(R.id.ll_content)
    LinearLayout llContent;
    @Bind(R.id.tv_new_car_push)
    TextView tvNewCarPush;
    @Bind(R.id.tv_push_car_price)
    TextView tvPushCarPrice;
    @Bind(R.id.tv_query_all)
    TextView tvQueryAll;
    @Bind(R.id.tv_evaluate_price)
    TextView tvEvaluatePrice;
    @Bind(R.id.tv_back_home)
    TextView tvBackHome;
    @Bind(R.id.tv_info)
    TextView tvInfo;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.iv_circle_point)
    ImageView ivCirclePoint;
    private ArrayList<PushCarEntity.VehicleSeriesListBean> list;
    private CommonAdapter<PushCarEntity.VehicleSeriesListBean> adapter;
    private ValuationQuery valuationQuery;
    private EvaluateResultPresenter presenter;
    private ValuationQuery[] valuationQuerys = new ValuationQuery[3];
    private boolean isFirst = true;
    private Double nextYearPurchasePrice;
    private Intent intent;
    private double price;
    private String valuationQueryRecordId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_evaluate_result;
    }

    @Override
    public void initView() {
        UMADplus.track(this, "估值结果页面", null);
        getIntentData();
        presenter = new EvaluateResultPresenter(this);
        initData();
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        list = new ArrayList<>();
        adapter = new CommonAdapter<PushCarEntity.VehicleSeriesListBean>(this, R.layout.item_push_car, list) {
            @Override
            protected void convert(ViewHolder holder, PushCarEntity.VehicleSeriesListBean data, int position) {
                ImageView imageView = holder.getView(R.id.iv_item);
                ImageLoadManager.getImageLoadManager().load(data.getCoverImage(), imageView);
                holder.setText(R.id.tv_item_title, data.getName());
                holder.setText(R.id.tv_item_subtitle, data.getSellPrice() + "万元起售");
            }
        };
        recyclerView.setAdapter(adapter);
        myselectview.setListener(this);
        presenter.findVehicleSeriesByResidualvalue(getWContext().get());
        adapter.setOnItemClickListener(this);
    }

    private void initData() {
        presenter.getNextYeayPrice(valuationQuery);
        tvCarType.setText(valuationQuery.getModelName());
        String date = valuationQuery.getFirstRegDate().replace("-", "年") + "月";
        tvCarInfo.setText(date + " | " + Utils.formatString((long) (valuationQuery.getMileage() * 10000)) + "公里" + " | " + valuationQuery.getCityName());
        if (valuationQuery.getNewCarGuidePrice() != 0) {
            tvNewCarPrice.setText("新车指导价：" + Utils.formatInt(valuationQuery.getNewCarGuidePrice() + "") + "万");
        }
    }


    @OnClick({R.id.tv_sell_car, R.id.tv_back_home, R.id.tv_query_all, R.id.tv_info})
    public void onClick(View view) {
        APP.getInstance().resetCountDown();
        switch (view.getId()) {
            case R.id.tv_sell_car://我要卖车
                Operation operation = getOperation();
                operation.addParameter("valuationQueryRecordIds", valuationQueryRecordId + "");
                operation.forward(InputPhoneActivity.class);
//                presenter.getfindByModelCode(getWContext().get());
//                getOperation().forward(FinancialProgramActivity.class);
                break;
            case R.id.tv_back_home:
                APP.getInstance().backHomeActivity();
                break;
            case R.id.tv_query_all://查看全部
                presenter.queryAllCar(getWContext().get());
                break;
            case R.id.tv_info://车况说明
                getOperation().forward(CarStatusInfoActivity.class);
                break;
        }
    }

    @Override
    public void stateChange(int state) {
        if (valuationQuerys[state] != null) {
            valuationQuery = valuationQuerys[state];
            freshenData(state);
        } else {
            presenter.getValuationData(getWContext().get(), state);
        }

        APP.getInstance().resetCountDown();
    }

    private void freshenData(int state) {
        myCharLine.initTime(valuationQuery.getQueryTime());
        double newCarPrice = valuationQuery.getNewCarGuidePrice();
        if (newCarPrice == 0) {
            newCarPrice = Double.valueOf(Utils.format2Right(valuationQuery.getPurchasePrice() * 1.15 + ""));
        }
        myCharLine.initPrice(newCarPrice, valuationQuery.getPurchasePrice(), nextYearPurchasePrice);
        NumAnim.startAnim(tvEvaluatePrice, Float.valueOf(Utils.formatInt(valuationQuery.getPurchasePrice() + "")));
        ivCirclePoint.animate().rotation(-(state - 1) * 60).setDuration(200).start();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && isFirst) {
            isFirst = false;
            stateChange(1);
        }
    }

    @Override
    public void setNextYearPrice(Double nextYearPurchasePrice) {
        this.nextYearPurchasePrice = nextYearPurchasePrice;
    }

    @Override
    public Intent getDataIntent() {
        return intent;
    }

    @Override
    public void setValuationData(ValuationQuery valuationQueryResult, int state) {
        valuationQueryRecordId = valuationQueryResult.getRecordId() + "";
        this.valuationQuery = valuationQueryResult;
        valuationQuerys[state] = valuationQuery;
        freshenData(state);
    }

    @Override
    public String getPrice() {
        return price + "";
    }

    @Override
    public void setAllCar(List<QueryAllCarType> data) {
        //TODO 全部车型的数据
        Intent intent = new Intent(this, AllCarTypeActivity.class);
        intent.putExtra("data", (Serializable) data);
        intent.putExtra("valuationQueryRecordId", valuationQueryRecordId);
        intent.putExtra("sellPrice", price * 10000);
        startActivity(intent);
    }

    @Override
    public void setPushCarData(List<PushCarEntity.VehicleSeriesListBean> data) {
        list.clear();
        list.addAll(data);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void setCarData(List<SellCarProgramEntity> data) {
        Intent intent = new Intent(this, FinancialProgramActivity.class);
        intent.putExtra("data", (Serializable) data);
        intent.putExtra("valuationQueryRecordId", valuationQueryRecordId);
        intent.putExtra("sellPrice", price * 10000);
        startActivity(intent);
    }

    @Override
    public void setTips(String tips) {
        tvPushCarPrice.setText(tips);
    }

    public void getIntentData() {
        intent = getIntent();
        valuationQuery = (ValuationQuery) intent.getSerializableExtra("data");
        valuationQuerys[1] = valuationQuery;
        price = valuationQuery.getPurchasePrice();
        valuationQueryRecordId = valuationQuery.getRecordId() + "";
    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        PushCarEntity.VehicleSeriesListBean pushCarEntity = list.get(position);

        UMADplus.track(this, "广告" + pushCarEntity.getName(), null);
        presenter.findVehicleByVehicleSeriesAndOnline(this, pushCarEntity.getId() + "");
        APP.getInstance().resetCountDown();
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }
}
